const pending = 'pending'
const fulfilled = 'fulfilled'
const rejected = 'rejected'

const execFuncCatchError = (execFunc, value, resolve, reject) => {
    try {
        const result = execFunc(value)
        resolve(result)
    } catch (error) {
        reject(error)
    }
}
class MyPromise {
    constructor(executor) {
        this.status = pending
        this.value = undefined
        this.reason = undefined

        this.onFulfilledFns = []
        this.onRejectedFns = []

        const resolve = (value) => {
            queueMicrotask(() => {
                if (this.status !== pending) return
                this.status = fulfilled
                this.value = value
                this.onFulfilledFns.forEach(fn => {
                    fn()
                })
            })

        }
        const reject = (reason) => {
            queueMicrotask(() => {
                if (this.status !== pending) return
                this.status = rejected
                this.reason = reason
                this.onRejectedFns.forEach(fn => {
                    fn()
                })
            })
        }
        try {
            executor(resolve, reject)
        } catch (error) {
            reject(error)
        }
    }
    then(onFulfilled, onRejected) {
        return new MyPromise((res, rej) => {
            // 1.状态fulfilled确定下来
            if (this.status === fulfilled && onFulfilled) {
                // try {
                //     const value = onFulfilled(this.value)
                //     res(value)
                // } catch (error) {
                //     rej(error)
                // }
                execFuncCatchError(onFulfilled, this.value, res, rej)

            }
            // 2.状态rejected确定下来
            if (this.status === rejected && onRejected) {
                // try {
                //     const reason = onRejected(this.reason)
                //     res(reason)
                // } catch (error) {
                //     rej(error)
                // }
                execFuncCatchError(onRejected, this.reason, res, rej)

            }
            // 3.还不知道状态
            if (this.status === pending) {
                this.onFulfilledFns.push(() => {
                    // try {
                    //     const value = onFulfilled(this.value)
                    //     res(value)
                    // } catch (error) {
                    //     rej(error)
                    // }
                    execFuncCatchError(onFulfilled, this.value, res, rej)

                })
                this.onRejectedFns.push(() => {
                    // try {
                    //     const reason = onRejected(this.reason)
                    //     res(error)
                    // } catch (error) {
                    //     rej(error)
                    // }
                    execFuncCatchError(onRejected, this.reason, res, rej)

                })
            }
        })

    }
}

const p1 = new MyPromise((resolve, reject) => {
    resolve(111)
    reject(222)
})

p1.then((res) => {
    console.log('res ' + res);
}, (err) => {
    console.log('err  ' + err);
})

p1.then((res) => {
    console.log('res2 ' + res);
    return 222
    // throw new Error('err messgae')
}, (err) => {
    console.log('err2  ' + err);
}).then((res) => {
    console.log('res4 ' + res);
}, (err) => {
    console.log('err4  ' + err);
})

setTimeout(() => {
    p1.then((res) => {
        console.log('res3 ' + res);
    }, (err) => {
        console.log('err3  ' + err);
    })
}, 1000)